package com.hk.employeemanagementsystem.web.controller;

import com.hk.employeemanagementsystem.entity.DepartmentEntity;
import com.hk.employeemanagementsystem.service.DepartmentEntityService;
import com.hk.employeemanagementsystem.util.web.WebCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author by 何坤
 * @Classname DepartmentController
 * @Description TODO
 * @Date 2020/2/7 17:30
 */
@RestController
@RequestMapping(value = "/dep")
public class DepartmentController {

    private Logger logger = LoggerFactory.getLogger(DepartmentController.class);

    @Autowired
    private DepartmentEntityService departmentEntityServiceImpl;

    @GetMapping("/getDepTree")
    public Map<String, Object> selectAllDepartmentTree(){
        Map<String, Object> tips = new HashMap<>();
        try {
            List<DepartmentEntity> department = departmentEntityServiceImpl.getDepartment();
            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_SELECT);
            tips.put("data", department);
        } catch (Exception e) {
            logger.info(e.getMessage());
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_SELECT);
        }
        return tips;
    }

    @GetMapping(value = "/get")
    public Map<String, Object> selectAllDepartment(){
        Map<String, Object> tips = new HashMap<>();
        try {
            List<DepartmentEntity> department = departmentEntityServiceImpl.list();
            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_SELECT);
            tips.put("data", department);
        } catch (Exception e) {
            logger.info(e.getMessage());
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_SELECT);
        }
        return tips;
    }

    @PostMapping(value = "/addDep")
    public Map<String, Object> addDepartment(@RequestBody DepartmentEntity departmentEntity){
        Map<String, Object> tips = new HashMap<>();
        try {
            if (departmentEntityServiceImpl.addDepartment(departmentEntity) == 1){
                tips.put(WebCode.SUCCESS, true);
                tips.put(WebCode.MSG, WebCode.SUCCESS_OPERATE);
                tips.put("data", departmentEntity);
            } else {
                tips.put(WebCode.SUCCESS, false);
                tips.put(WebCode.MSG, WebCode.FAILURE_OPERATE);
            }
        } catch (Exception e) {
            logger.info(e.getMessage());
            e.printStackTrace();
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_OPERATE);
        }
        return tips;
    }

    @DeleteMapping(value = "/get/{id}")
    public Map<String, Object> deleteDepartment(@PathVariable Integer id){
        Map<String, Object> tips = new HashMap<>();
        try {
            if (departmentEntityServiceImpl.deleteDepartment(id) == 1){
                tips.put(WebCode.SUCCESS, true);
                tips.put(WebCode.MSG, WebCode.SUCCESS_DELETE);
            } else {
                tips.put(WebCode.SUCCESS, false);
                tips.put(WebCode.MSG, WebCode.FAILURE_DELETE);
            }
        } catch (Exception e) {
            logger.info(e.getMessage());
            e.printStackTrace();
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_DELETE);
        }
        return tips;
    }
}
